
var api = require('../../utils/networkApi.js');
var commonMethod = require("../../utils/commonMethod.js"); 

var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    passWord:"",
    userName:"",
    isCheck:true,
    currentIndex:0
  },

  checkBoxSave:function(){
    console.log(this.data.isCheck);
    if (this.data.isCheck){
      this.setData({
        isCheck: false
      });
    }else{
      this.setData({
        isCheck: true
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('isCheck')){
      this.setData({
        userName: wx.getStorageSync('userName'),
        passWord: wx.getStorageSync('passWord')
      })
    }
   
  },

/**
 * 登录 表单提交
 */
formSubmit: function (e) {
  //去除空格符
  var account = commonMethod.filterSpace(e.detail.value.userName);
  var passWord = commonMethod.filterSpace(e.detail.value.passWord);
  var that=this;
  var userSourceIndex=2;//默认企业账号登录
  if(that.data.currentIndex==0){
    userSourceIndex = 2;//企业账号
  } else if (that.data.currentIndex == 1){
    userSourceIndex = 3;//员工账号
  }
  //验证用户名和密码非空
  if (account == '') {
    wx.showToast({
      title: '用户名不能为空',
      icon: "none"
    })
    return;
  } else if (!myreg.test(account)) {
    wx.showToast({
      title: '手机号格式不正确！',
      icon: "none"
    })
    return;
  }  else if (passWord == '') {
    wx.showToast({
      title: '密码不能为空',
      icon: "none"
    })
    return;
  }else{

    //获取设备信息
    let sys = wx.getSystemInfoSync();
    console.log(dataBody);
    
    //获取token
    var dataBody = {
      'channel': sys.platform,
      'cver': sys.version,
      'mid': '23nlkx2',
      'memo': sys.model,
      'canal': sys.system,
      'os': sys.model,
      'p': 1,
      'userSource': 11
    }
    

    api.networkForData("deviceReg", dataBody)
      .then(res => {
        wx.setStorageSync('userToken', res.token);
        //登录
        var dataBody = {
          'userName': account,
          'password': passWord,
          'userSource': 11,
          'userType': userSourceIndex
        }

        console.log(dataBody);

        api.networkForData("login", dataBody)
          .then(res => {
            if (res.user.authStatus==2){//认证成功状态
              if (res.user.authType == 2) {//企业用户
                if (res.user.companyType == 1 || res.user.companyType == 5) {//货代公司、物流科登录
                  //存储密码
                  if (that.data.isCheck) {
                    wx.setStorageSync('userName', account);
                    wx.setStorageSync('passWord', passWord);
                    //存储token
                    wx.setStorageSync('userToken', res.token);

                    wx.setStorageSync('realName', res.user.realName);
                  }
                  wx.setStorageSync('isCheck', that.data.isCheck);
                  //到主页
                  wx.reLaunch({ url: '../publishOrder/publishOrder', });
                } else {
                  wx.showToast({
                    title: '此账号暂无权限登录！',
                    icon: "none"
                  })
                }
              }else{
                wx.showToast({
                  title: '此账号暂无权限登录！',
                  icon: "none"
                })
              }
            }else{
              wx.showToast({
                title: '此账号暂未通过审核！',
                icon: "none"
              })
            }
          })
      })
  }

},

  //用户点击tab时调用
  titleClick: function (e) {
    let currentPageIndex =
      this.setData({
        //拿到当前索引并动态改变
        currentIndex: e.currentTarget.dataset.idx
      })
  },
  //swiper切换时会调用
  pagechange: function (e) {
    if ("touch" === e.detail.source) {
      let currentPageIndex = this.data.currentIndex
      currentPageIndex = (currentPageIndex + 1) % 2
      this.setData({
        currentIndex: currentPageIndex
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
